/* 
 * Created on 2006-5-7
 * Last modified on 2006-5-7
 * Powered by GamVan.com
 */
package com.litxuf.logic.core.conn;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;



@SuppressWarnings("unchecked")
public class HibernateConn {
	
	
    private static SessionFactory sessionFactory = null;
    
    public static final ThreadLocal localSession = new ThreadLocal();
    
    public static void init() throws HibernateException {
        Configuration config = new Configuration().configure();
        sessionFactory = config.buildSessionFactory();
    }
    
     
	public static Session getSession(){

		if(sessionFactory ==null) init();
		        Session session = (Session)localSession.get();
		        if(session==null){
		            session = sessionFactory.openSession();
		            localSession.set(session);
		        }
		        return session;
		    }

	 public static void closeSession() throws HibernateException {    
	        Session session = (Session)localSession.get();
	        localSession.set(null);
	        if (session != null){
	            session.close(); 
	        }
	    }
    public static void main(String[] args) {
	//	init();
	Session session=getSession();
	if(session!=null)System.out.print(session.connection());
	}
}
